package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.bean.Orders;
import com.example.reggie.service.OrdersService;
import com.example.reggie.utils.BaseContext;
import com.example.reggie.utils.R;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;


    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(number==null?false:true,Orders::getNumber,number)
                .gt(beginTime==null?false:true,Orders::getOrderTime,beginTime)
                .lt(endTime==null?false:true,Orders::getOrderTime,beginTime);
        ordersService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orders.setUserId(BaseContext.getId());
        ordersService.submit(orders);
        return R.success("提交成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        Page<Orders> pageInfo=new Page<>();
        ordersService.page(pageInfo);
        return R.success(pageInfo);
    }
}
